package com.kalix.dataPlatform.right.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.dataPlatform.right.api.biz.IWaringListBeanService;
import com.kalix.dataPlatform.right.api.dao.IWaringListBeanDao;
import com.kalix.dataPlatform.right.api.model.InfUtil;
import com.kalix.dataPlatform.right.entities.WaringListBean;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;

public class WaringListBeanServiceImpl extends GenericBizServiceImpl<IWaringListBeanDao, WaringListBean> implements IWaringListBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String Port =(String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    @Override
    public String getWaringListValue() {
        String url = "http://"+Ip+":"+Port+"/query/mo/AlarmEvent?where=(alarmStatus=2%20or%20alarmStatus=3)%20";
        String da= InfUtil.interfaceUtil(url,"");
        JSONArray array  = new JSONArray(da);
        JSONArray data = new JSONArray();
        for (int i = 0; i < array.length(); i++) {
            JSONObject ob  = (JSONObject) array.get(i);
//            String name = ob.getString("name");// 获取告警信息设备名称
            String levelValue = "";
            JSONArray properties = ob.getJSONArray("properties");
            String level = getLevel(properties);
            if ("1".equals(level)){
                levelValue = "提示";
            }else if("2".equals(level)) {
                levelValue = "低级";
            }else if("3".equals(level)) {
                levelValue = "中级";
            }else if("4".equals(level)) {
                levelValue = "高级";
            }else if("5".equals(level)) {
                levelValue = "紧急";
            }
            String alarmContext = getAlarmContext(properties);
            String[]  get = alarmContext.split("，");
            JSONObject obj = new JSONObject();
            obj.put("level",levelValue); // 告警级别
            obj.put("location",get[0]); // 告警描述
            obj.put("status",get[1]); // 告警状态
            data.put(obj);
        }
        JSONObject jsonObject = new JSONObject();
        JSONObject objLevel = new JSONObject();
        objLevel.put("name","告警级别");
        JSONObject objDescribe = new JSONObject();
        objDescribe.put("name","告警描述");
        JSONArray arr =new JSONArray();
        arr.put(objLevel);
        arr.put(objDescribe);
        jsonObject.put("title","当前警告信息");
        jsonObject.put("thead",arr);
        jsonObject.put("data",data);
        return jsonObject.toString();
    }

    public String getLevel(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("level".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof Integer) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }

    public String getAlarmContext(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("alarmContext".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof String) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }
}
